package com.easondongh.business.controller;

import com.easondongh.business.domain.Goods;
import com.easondongh.business.service.GoodsService;
import com.easondongh.business.vo.GoodsQueryVo;
import com.easondongh.common.enums.ResponseEnum;
import com.easondongh.common.response.ResponseObj;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author EasonDongH
 * @date 2020/6/1 10:52
 */
@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 新增商品
     * @param goods
     * @return
     */
    @PostMapping("/addGoods")
    public ResponseObj addGoods(@RequestBody Goods goods) {
        if (goods == null || StringUtils.isBlank(goods.getGoodsName()) ) {
            log.warn("商品：新增时商品名称不能为空");
            return new ResponseObj(ResponseEnum.ADD_FAIL);
        }
        this.goodsService.save(goods);
        return new ResponseObj(ResponseEnum.ADD_SUCCESS);
    }

    /**
     * 修改商品
     * @param goods
     * @return
     */
    @PutMapping("updateGoods")
    public ResponseObj updateGoods(@RequestBody Goods goods) {
        if (goods == null || StringUtils.isBlank(goods.getGoodsName())) {
            log.warn("商品：更新时id、商品名称均不能为空");
            return new ResponseObj(ResponseEnum.UPDATE_FAIL);
        }
        this.goodsService.updateById(goods);
        return new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 批量删除商品
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds")
    public ResponseObj deleteByIds(@RequestBody List<Integer> ids) {
        if (ids != null && ids.size() > 0) {
            this.goodsService.removeByIds(ids);
        }
        return new ResponseObj(ResponseEnum.DELETE_SUCCESS);
    }

    /**
     * 分页查询商品
     * 用于后台查询，会将未启用的商品页查询出来
     * @return
     */
    @PostMapping("/getByPage")
    public ResponseObj<PageResult<Goods>> getByPage(@RequestBody BasePageQueryVo<GoodsQueryVo> queryCondition) {
        if(queryCondition == null) {
            return ResponseObj.QueryFail;
        }
        PageResult<Goods> result = this.goodsService.getByPage(queryCondition);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, result);
    }

    /**
     * 启用当前商品
     * @param id
     * @return
     */
    @PutMapping("/enable")
    public ResponseObj enableById(Integer id) {
        if(id != null) {
            this.goodsService.enableById(id);
        }
        return new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 弃用当前商品
     * @param id
     * @return
     */
    @PutMapping("/disable")
    public ResponseObj disableById(Integer id) {
        if(id != null) {
            this.goodsService.disableById(id);
        }
        return new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    }

    @GetMapping("/getAllEnableGoods")
    public  ResponseObj<List<Goods>> getAllEnableGoods() {
        List<Goods> goodsList = this.goodsService.getAllEnableGoods();
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, goodsList);
    }
}
